package ute.mvc.service;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import ute.mvc.dao.AssignmentDAO;
import ute.mvc.dao.Assignment_StudentDAO;
import ute.mvc.dao.ClassDAO;
import ute.mvc.dao.FileDAO;
import ute.mvc.dao.SubmittedAssignmentDAO;
import ute.mvc.dto.StudentAssignmentDTO;
import ute.mvc.dto.SubmittedAssignmentDTO;
import ute.mvc.model.Assignment;
import ute.mvc.model.Assignment_Student;
import ute.mvc.model.MyClass;
import ute.mvc.model.MyFile;
import ute.mvc.model.SubmittedAssignment;
import ute.mvc.model.User;
import ute.mvc.util.FileUtil;

@Service("submittedAssignmentService")
public class SubmittedAssignmentServiceImpl implements SubmittedAssignmentService{
	
	@Autowired
	SubmittedAssignmentDAO submittedAssignmentDAO;
	@Autowired
	Assignment_StudentDAO assignment_StudentDAO;
	@Autowired
	AssignmentDAO assignmentDAO;
	@Autowired
	FileDAO fileDAO;
	@Autowired
	ClassDAO classDAO;
	@Autowired
	ApplicationContext applicationContext;
	
	public void addSubmittedAssignment(SubmittedAssignment submittedAssignment) {
		submittedAssignmentDAO.addSubmittedAssignment(submittedAssignment);
	}

	public void updateSubmittedAssignment(
			SubmittedAssignment submittedAssignment) {
		submittedAssignmentDAO.updateSubmittedAssignment(submittedAssignment);
	}

	public void deleteSubmittedAssignment(
			SubmittedAssignment submittedAssignment) {
		submittedAssignmentDAO.deleteSubmittedAssignment(submittedAssignment);
	}

	public SubmittedAssignment getSubmittedAssignment(
			String submittedAssignmentId) {
		
		
		return submittedAssignmentDAO.getSubmittedAssignment(submittedAssignmentId);
	}

	public SubmittedAssignment submit(String assignmentId, int studentId,
			StudentAssignmentDTO studentAssignmentDTO) {
		String submittedAssignmentId = assignmentId + "_" + studentId;
		Assignment assignment = assignmentDAO.getAssignmentById(assignmentId);
		Assignment_Student assignment_Student = assignment_StudentDAO.getAssignment_Student(submittedAssignmentId);
		
		SubmittedAssignment submittedAssignment = submittedAssignmentDAO.getSubmittedAssignment(submittedAssignmentId);
		
		if(submittedAssignment == null){
			submittedAssignment = new SubmittedAssignment();
			submittedAssignment.setSubmittedAssignmentId(submittedAssignmentId);
			submittedAssignment.setAssignment_Student(assignment_Student);
			submittedAssignment.setDescription(studentAssignmentDTO.getDescription());
			submittedAssignment.setSubmittedDate(new Date());
			submittedAssignment.setSubmittedTime(1);
			submittedAssignmentDAO.addSubmittedAssignment(submittedAssignment);
		} else {
			submittedAssignment.setDescription(studentAssignmentDTO.getDescription());
			submittedAssignment.setSubmittedDate(new Date());
			submittedAssignment.setSubmittedTime(submittedAssignment.getSubmittedTime() + 1);
		}
		
		String fileName = studentAssignmentDTO.getFile().getOriginalFilename();
		if (!"".equals(fileName)) {
			// add new file to DB
			MyFile file = new MyFile();
			file.setFileName(fileName);
			fileDAO.addFile(file);

			// write this file to directory
			MultipartFile mFile = studentAssignmentDTO.getFile();

			try {
				mFile.transferTo(new java.io.File(FileUtil.getFileLocation(applicationContext) + "\\" + file.getFileId()));
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			// set this file to new Assignment
			submittedAssignment.setFile(file);
		}
		submittedAssignmentDAO.updateSubmittedAssignment(submittedAssignment);
		
		return null;
	}

	public List<SubmittedAssignmentDTO> getListSubmittedAssignmentDTO(
			String assignmentId, String classId) {
		System.out.println("lay class");
		List<SubmittedAssignmentDTO> result = new ArrayList<SubmittedAssignmentDTO>();
		
		MyClass mClass = classDAO.getClassById(classId);
		System.out.println(mClass.getClassName());
		List<User> listStudent = (List<User>) mClass.getListStudent();
		
		String submittedAssignmentId;
		SubmittedAssignment sa;
		
		for(User u : listStudent) {
			submittedAssignmentId = assignmentId + "_" + u.getUserId();
			sa = submittedAssignmentDAO.getSubmittedAssignment(submittedAssignmentId);
			if(sa != null) {
				SubmittedAssignmentDTO dto = new SubmittedAssignmentDTO();
				dto.setSubmittedAssignmentId(sa.getSubmittedAssignmentId());
				dto.setEmail(u.getEmail());
				dto.setStudentName(u.getName());
				dto.setSubmitTime(sa.getSubmittedDate());
				dto.setPoint(sa.getPoint());
				
				result.add(dto);
			}
		}
		
		return result;
	}

}
